
package com.example.zydraw;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.zydraw.draw.IDrawPolicy;
import com.example.zydraw.draw.RoleRecord;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

public class CreateNewDialogFragment extends BottomSheetDialogFragment implements EditPaintDialogFragment.PaintCallback, TextWatcher {

    EditText startX;
    EditText startY;
    EditText endX;
    EditText endY;

    IDrawPolicy iDrawPolicy;
    UpdateListener updateListener;

    public CreateNewDialogFragment(IDrawPolicy iDrawPolicy, UpdateListener updateListener) {
        this.iDrawPolicy = iDrawPolicy;
        this.updateListener = updateListener;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_create_new_dialog_layout, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        startX = view.findViewById(R.id.et_start_x);
        startY = view.findViewById(R.id.et_start_y);
        endX = view.findViewById(R.id.et_end_x);
        endY = view.findViewById(R.id.et_end_y);

        startX.addTextChangedListener(this);
        startY.addTextChangedListener(this);
        endX.addTextChangedListener(this);
        endY.addTextChangedListener(this);

        view.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismissAllowingStateLoss();
            }
        });
        view.findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismissAllowingStateLoss();
            }
        });

        view.findViewById(R.id.btn_edit_paint).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new EditPaintDialogFragment(iDrawPolicy.getPaintRecord(), CreateNewDialogFragment.this).show(getParentFragmentManager(), "EditPaintDialogFragment");
            }
        });
    }

    @Override
    public void update() {
        RoleRecord roleRecord = iDrawPolicy.getRoleRecord();

        roleRecord.setStartY(parseInt(startX.getText().toString()));
        roleRecord.setStartY(parseInt(startY.getText().toString()));
        roleRecord.setEndX(parseInt(endX.getText().toString()));
        roleRecord.setEndY(parseInt(endY.getText().toString()));

        updateListener.update();
    }

    private int parseInt(String str) {
        if (str == null) {
            return 0;
        }

        if (str.isEmpty() || str.isBlank()) {
            return 0;
        }

        return Integer.parseInt(str);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        update();
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    interface UpdateListener {
        void update();
    }
}
